package com.example.imtoken.modules.main.wallet.assets

import androidx.lifecycle.MutableLiveData
import com.example.imtoken.core.base.BaseViewModel
import com.example.imtoken.core.http.BaseObserver
import com.example.imtoken.core.logic.network.RetrofitUtils
import com.example.imtoken.entitys.http.BalanceEntity
import com.example.imtoken.entitys.http.ResponseEntity
import com.example.imtoken.room.base.AppDataBase
import com.example.imtoken.room.schema.WalletAddressSchema
import io.reactivex.schedulers.Schedulers
import java.util.*

class AssetsViewModel : BaseViewModel() {

    private var dataLiveData: MutableLiveData<MutableList<WalletAddressSchema>> = MutableLiveData()

    fun getDataLiveData(): MutableLiveData<MutableList<WalletAddressSchema>> {
        return dataLiveData
    }

    val refreshStatus = MutableLiveData(false)


    fun getWalletAddress() {
        refreshStatus.postValue(true)
        val appLong = AppDataBase.instance.getAppLoginDao().getAppLogin()
        val walletChecked =
            AppDataBase.instance.getWalletCheckedDao().getCheckedWallet(appLong.walletAddress)
        val account = AppDataBase.instance.getAccountDao().getDefAccount(appLong.walletAddress)
        var walletAddress = AppDataBase.instance.getWalletAddressDao()
            .getAccountWalletAddress(walletChecked?.address)
        if (walletAddress == null) walletAddress = ArrayList<WalletAddressSchema>()
        walletAddress.add(
            0,
            WalletAddressSchema(
                walletChecked?.icon,
                walletChecked?.walletType,
                walletChecked?.description,
                walletChecked?.address,
                walletChecked?.description,
                walletChecked?.address
            )
        )
        RetrofitUtils.apiService?.balance(account.coin)
            ?.subscribeOn(Schedulers.io())
            ?.unsubscribeOn(Schedulers.io())
            ?.subscribe(object : BaseObserver<ResponseEntity<BalanceEntity>>() {
                override fun onSuccess(value: ResponseEntity<BalanceEntity>) {
                    refreshStatus.postValue(false)
                    if (value.code == 0) {
                        if (value.data != null) {
                            for (address in walletAddress) {
                                for (v in value.data) {
                                    if (address.walletType == v.coin) {
                                        address.amount = v.amount
                                        address.price = v.price
                                        break
                                    }
                                }
                            }
                        }
                        dataLiveData.postValue(walletAddress)
                    } else {
                        mErrorStr.postValue(value.msg)
                    }
                }

                override fun onFailed(e: Throwable) {
                    refreshStatus.postValue(false)
                }

            })
    }

}